package com.kcn.xml;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import jxl.read.biff.BiffException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;

import com.kcn.utils.PropertiesReader;
public class Doc2XML {
	public int BuildXMLDOC(String filename) throws IOException,JDOMException{
		System.out.println("Doc2XML START");
		ExcelReader er=new ExcelReader();
		List<Invoice> invoices=new ArrayList<Invoice>();
		try {
			invoices=er.read(filename);
		} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Element root=new Element("body");
		Document doc=new Document(root);
		Element head=new Element("head");
		Element nsrsbh=new Element("nsrsbh").setText("120105000000001");
		Element nsrmc=new Element("nsrmc").setText("凯诺斯(中国)铝酸盐技术有限公司");
		Element dcrq=new Element("dcrq").setText(new SimpleDateFormat("yyyyMMdd").format(new Date()));
		Calendar calendar=Calendar.getInstance();

		calendar.set(calendar.YEAR, Integer.parseInt(er.getYear()));
		calendar.set(calendar.MONTH,Integer.parseInt(er.getMonth()));
		System.out.println(calendar.getActualMaximum(calendar.DAY_OF_MONTH));
		System.out.println(calendar.getActualMinimum(calendar.DAY_OF_MONTH));
		Element sssq_q=new Element("sssq_q").setText(er.getYear()+er.getMonth()+"01");
		Element sssq_z=new Element("sssq_z").setText(er.getYear()+er.getMonth()+calendar.getActualMaximum(calendar.DAY_OF_MONTH));
		Element totalrecords=new Element("records").setText(Integer.toString(invoices.size()));
		head.addContent(nsrsbh);
		head.addContent(nsrmc);
		head.addContent(dcrq);
		head.addContent(sssq_q);
		head.addContent(sssq_z);
		head.addContent(totalrecords);
		root.addContent(head);
		Element data=new Element("data");
		root.addContent(data);
		for(int i=0;i<invoices.size();i++){
			Element records=new Element("record");
			records.setAttribute("id", ""+i);
			Invoice iv=null;
			iv=invoices.get(i);
			records.addContent(new Element("fpdm").setText(iv.getFpdm()));
			records.addContent(new Element("fpmc").setText(iv.getFpmc()));
			records.addContent(new Element("fphm").setText(iv.getFphm()));
			records.addContent(new Element("kprq").setText(this.formator(iv.getKprq())));
			records.addContent(new Element("fkfmc").setText(iv.getFkfmc()));
			records.addContent(new Element("jehj").setText(iv.getJehj()));
			records.addContent(new Element("kplx").setText(iv.getKplx()));
			records.addContent(new Element("yfpdm").setText(iv.getYfpdm()));
			records.addContent(new Element("yfphm").setText(iv.getYfphm()));
			records.addContent(new Element("pm").setText(iv.getPm()));
			records.addContent(new Element("dw").setText(iv.getDw()));
			records.addContent(new Element("dj").setText(iv.getDj()));
			records.addContent(new Element("sl").setText(iv.getSl()));
			records.addContent(new Element("je").setText(iv.getJe()));
			data.addContent(records);
		}
		XMLOutputter xmloutput=new XMLOutputter();
		String xmlPath=PropertiesReader.getPropertiesReader().getProperty("downloadPath");
//		System.out.println(filename+filename.split("\\.").length);
//		filename=filename.split("\\.")[0]+".xml";
		String xmlfilename="xtfpxx_"+"121212121"+new SimpleDateFormat("yyyyMMdd").format(new Date())+".xml";
		FileOutputStream fps=new FileOutputStream(xmlPath+xmlfilename);
		xmloutput.output(doc, fps);
		fps.close();
		System.out.println("DOC2XML END");
		return invoices.size();
	}
public String formator(Date date){
	SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
	String kprq=sdf.format(date);
	return kprq;
}
//	public static void main(String args[]){
//		try{
//			Doc2XML d2x=new Doc2XML();
//			d2x.BuildXMLDOC();
//		}catch (Exception e) {
//			// TODO: handle exception
//			e.printStackTrace();
//		}
//	}
}
